{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import print_function\n",
    "import torch"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'0.4.1'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.version.__version__"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PyTorch basics"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The torch package contains data structures for multi-dimensional tensors and mathematical operations over these are\n",
    "defined. Additionally, it provides many utilities for efficient serializing of Tensors and arbitrary types, and other useful\n",
    "utilities.\n",
    "It has a CUDA counterpart, that enables you to run your tensor computations on an NVIDIA GPU with compute\n",
    "capability >= 2.0."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "x = [12,23,34,45,56,67,78]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.is_tensor(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.is_storage(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "y = torch.randn(1,2,3,4,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.is_tensor(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "False"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.is_storage(y)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "120"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.numel(y) # the total number of elements in the input Tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.zeros(4,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "16"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.numel(torch.zeros(4,4))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 0., 0.],\n",
       "        [0., 1., 0.],\n",
       "        [0., 0., 1.]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.eye(3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 0., 0., 0., 0.],\n",
       "        [0., 1., 0., 0., 0.],\n",
       "        [0., 0., 1., 0., 0.],\n",
       "        [0., 0., 0., 1., 0.],\n",
       "        [0., 0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.eye(5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 0., 0., 0.],\n",
       "        [0., 1., 0., 0.],\n",
       "        [0., 0., 1., 0.]])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.eye(3,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 0., 0., 0.],\n",
       "        [0., 1., 0., 0.],\n",
       "        [0., 0., 1., 0.],\n",
       "        [0., 0., 0., 1.],\n",
       "        [0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.eye(5,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "list"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "type(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "x1 = np.array(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([12, 23, 34, 45, 56, 67, 78])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([12, 23, 34, 45, 56, 67, 78])"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.from_numpy(x1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 2.0000,  2.3333,  2.6667,  3.0000,  3.3333,  3.6667,  4.0000,  4.3333,\n",
       "         4.6667,  5.0000,  5.3333,  5.6667,  6.0000,  6.3333,  6.6667,  7.0000,\n",
       "         7.3333,  7.6667,  8.0000,  8.3333,  8.6667,  9.0000,  9.3333,  9.6667,\n",
       "        10.0000])"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.linspace(2, 10, steps=25) #linear spacing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-10.0000,  -8.5714,  -7.1429,  -5.7143,  -4.2857,  -2.8571,  -1.4286,\n",
       "          0.0000,   1.4286,   2.8571,   4.2857,   5.7143,   7.1429,   8.5714,\n",
       "         10.0000])"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.linspace(-10, 10, steps=15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1.0000e-10, 2.6827e-09, 7.1969e-08, 1.9307e-06, 5.1795e-05, 1.3895e-03,\n",
       "        3.7276e-02, 1.0000e+00, 2.6827e+01, 7.1968e+02, 1.9307e+04, 5.1795e+05,\n",
       "        1.3895e+07, 3.7276e+08, 1.0000e+10])"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.logspace(start=-10, end=10, steps=15) #logarithmic spacing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1., 1., 1., 1.])"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.ones(4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[1., 1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1., 1.],\n",
       "        [1., 1., 1., 1., 1.]])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.ones(4,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0.4518, 0.5653, 0.6348, 0.6437, 0.2143, 0.9604, 0.3669, 0.4934, 0.5334,\n",
       "        0.1810])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# random numbers from a uniform distribution between the values \n",
    "# 0 and 1\n",
    "torch.rand(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.3734, 0.0354, 0.0399, 0.4571, 0.8493],\n",
       "        [0.0460, 0.9193, 0.2551, 0.1654, 0.2708],\n",
       "        [0.8007, 0.2927, 0.7814, 0.1313, 0.4698],\n",
       "        [0.1168, 0.0692, 0.7984, 0.0008, 0.9422]])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.rand(4, 5) \n",
    "# random values between 0 and 1 and fillied with a matrix of \n",
    "# size rows 4 and columns 5"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([ 0.6038,  0.1653,  0.4317, -1.2466,  0.0525, -1.0223,  0.4983,  0.6985,\n",
       "         0.1422, -0.5592])"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#random numbers from a normal distribution, \n",
    "#with mean =0 and standard deviation =1\n",
    "torch.randn(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.7288,  2.1191,  0.8396,  1.3021, -0.7470],\n",
       "        [ 0.4583, -0.0548, -0.0256,  0.7262, -0.2352],\n",
       "        [ 0.4478, -1.0081,  0.2807, -0.7128,  0.2257],\n",
       "        [-0.6427,  0.7345,  0.3246, -1.6767,  0.2152]])"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.randn(4, 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([6, 4, 1, 0, 2, 9, 8, 7, 5, 3])"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#selecting values from a range, this is called random permutation\n",
    "torch.randperm(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38])"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#usage of range function \n",
    "torch.arange(10, 40,2) #step size 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27,\n",
       "        28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.arange(10,40) #step size 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.2959, -2.6443,  0.8257, -0.8048,  1.6426],\n",
       "        [ 1.2577, -0.5935, -0.5804,  0.1882, -0.0432],\n",
       "        [-0.4921,  0.2224,  1.0330,  0.4772,  1.2407],\n",
       "        [-1.2843, -1.3309, -1.1851, -0.4932,  1.4307]])"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d = torch.randn(4, 5)\n",
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([1, 1, 0, 1])"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.argmin(d,dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([4, 0, 4, 4])"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.argmax(d,dim=1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0., 0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0., 0.],\n",
       "        [0., 0., 0., 0., 0.]])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# create a 2dtensor filled with values as 0\n",
    "torch.zeros(4,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# create a 1d tensor filled with values as 0\n",
    "torch.zeros(10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [],
   "source": [
    "#indexing and performing operation on the tensors\n",
    "x = torch.randn(4,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.9737, -0.3976, -0.4175, -1.4037, -0.2938],\n",
       "        [-0.8564,  0.8878,  0.4645, -0.3647,  2.2762],\n",
       "        [-2.3109, -0.6249, -1.1965, -0.3033,  0.1784],\n",
       "        [-1.0587,  1.1527, -1.6724,  2.2054, -0.5828]])"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.9737, -0.3976, -0.4175, -1.4037, -0.2938],\n",
       "        [-0.8564,  0.8878,  0.4645, -0.3647,  2.2762],\n",
       "        [-2.3109, -0.6249, -1.1965, -0.3033,  0.1784],\n",
       "        [-1.0587,  1.1527, -1.6724,  2.2054, -0.5828],\n",
       "        [ 0.9737, -0.3976, -0.4175, -1.4037, -0.2938],\n",
       "        [-0.8564,  0.8878,  0.4645, -0.3647,  2.2762],\n",
       "        [-2.3109, -0.6249, -1.1965, -0.3033,  0.1784],\n",
       "        [-1.0587,  1.1527, -1.6724,  2.2054, -0.5828]])"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#concatenate two tensors\n",
    "torch.cat((x,x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.9737, -0.3976, -0.4175, -1.4037, -0.2938],\n",
       "        [-0.8564,  0.8878,  0.4645, -0.3647,  2.2762],\n",
       "        [-2.3109, -0.6249, -1.1965, -0.3033,  0.1784],\n",
       "        [-1.0587,  1.1527, -1.6724,  2.2054, -0.5828],\n",
       "        [ 0.9737, -0.3976, -0.4175, -1.4037, -0.2938],\n",
       "        [-0.8564,  0.8878,  0.4645, -0.3647,  2.2762],\n",
       "        [-2.3109, -0.6249, -1.1965, -0.3033,  0.1784],\n",
       "        [-1.0587,  1.1527, -1.6724,  2.2054, -0.5828],\n",
       "        [ 0.9737, -0.3976, -0.4175, -1.4037, -0.2938],\n",
       "        [-0.8564,  0.8878,  0.4645, -0.3647,  2.2762],\n",
       "        [-2.3109, -0.6249, -1.1965, -0.3033,  0.1784],\n",
       "        [-1.0587,  1.1527, -1.6724,  2.2054, -0.5828]])"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#concatenate n times based on array size\n",
    "torch.cat((x,x,x))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.9737, -0.3976, -0.4175, -1.4037, -0.2938,  0.9737, -0.3976, -0.4175,\n",
       "         -1.4037, -0.2938,  0.9737, -0.3976, -0.4175, -1.4037, -0.2938],\n",
       "        [-0.8564,  0.8878,  0.4645, -0.3647,  2.2762, -0.8564,  0.8878,  0.4645,\n",
       "         -0.3647,  2.2762, -0.8564,  0.8878,  0.4645, -0.3647,  2.2762],\n",
       "        [-2.3109, -0.6249, -1.1965, -0.3033,  0.1784, -2.3109, -0.6249, -1.1965,\n",
       "         -0.3033,  0.1784, -2.3109, -0.6249, -1.1965, -0.3033,  0.1784],\n",
       "        [-1.0587,  1.1527, -1.6724,  2.2054, -0.5828, -1.0587,  1.1527, -1.6724,\n",
       "          2.2054, -0.5828, -1.0587,  1.1527, -1.6724,  2.2054, -0.5828]])"
      ]
     },
     "execution_count": 43,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#concatenate n times based on array size, over column\n",
    "torch.cat((x,x,x),1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.9737, -0.3976, -0.4175, -1.4037, -0.2938],\n",
       "        [-0.8564,  0.8878,  0.4645, -0.3647,  2.2762],\n",
       "        [-2.3109, -0.6249, -1.1965, -0.3033,  0.1784],\n",
       "        [-1.0587,  1.1527, -1.6724,  2.2054, -0.5828],\n",
       "        [ 0.9737, -0.3976, -0.4175, -1.4037, -0.2938],\n",
       "        [-0.8564,  0.8878,  0.4645, -0.3647,  2.2762],\n",
       "        [-2.3109, -0.6249, -1.1965, -0.3033,  0.1784],\n",
       "        [-1.0587,  1.1527, -1.6724,  2.2054, -0.5828]])"
      ]
     },
     "execution_count": 44,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#concatenate n times based on array size, over rows\n",
    "torch.cat((x,x),0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {},
   "outputs": [],
   "source": [
    "#how to split a tensor among small chunks"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Help on built-in function chunk:\n",
      "\n",
      "chunk(...)\n",
      "    chunk(tensor, chunks, dim=0) -> List of Tensors\n",
      "    \n",
      "    Splits a tensor into a specific number of chunks.\n",
      "    \n",
      "    Last chunk will be smaller if the tensor size along the given dimension\n",
      "    :attr:`dim` is not divisible by :attr:`chunks`.\n",
      "    \n",
      "    Arguments:\n",
      "        tensor (Tensor): the tensor to split\n",
      "        chunks (int): number of chunks to return\n",
      "        dim (int): dimension along which to split the tensor\n",
      "\n"
     ]
    }
   ],
   "source": [
    "help(torch.chunk)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[ 2.4830, -0.9584,  1.5975,  0.0509],\n",
      "        [ 0.4128, -1.7760, -0.6675, -1.0888],\n",
      "        [ 0.9758,  0.0496,  0.0805, -0.2817],\n",
      "        [ 0.0582,  2.5004,  1.0438, -0.6525]])\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "(tensor([[ 2.4830, -0.9584,  1.5975,  0.0509],\n",
       "         [ 0.4128, -1.7760, -0.6675, -1.0888]]),\n",
       " tensor([[ 0.9758,  0.0496,  0.0805, -0.2817],\n",
       "         [ 0.0582,  2.5004,  1.0438, -0.6525]]))"
      ]
     },
     "execution_count": 47,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a = torch.randn(4, 4)\n",
    "print(a)\n",
    "torch.chunk(a,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 2.4830, -0.9584,  1.5975,  0.0509],\n",
       "         [ 0.4128, -1.7760, -0.6675, -1.0888]]),\n",
       " tensor([[ 0.9758,  0.0496,  0.0805, -0.2817],\n",
       "         [ 0.0582,  2.5004,  1.0438, -0.6525]]))"
      ]
     },
     "execution_count": 48,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.chunk(a,2,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([[ 2.4830, -0.9584],\n",
       "         [ 0.4128, -1.7760],\n",
       "         [ 0.9758,  0.0496],\n",
       "         [ 0.0582,  2.5004]]), tensor([[ 1.5975,  0.0509],\n",
       "         [-0.6675, -1.0888],\n",
       "         [ 0.0805, -0.2817],\n",
       "         [ 1.0438, -0.6525]]))"
      ]
     },
     "execution_count": 49,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.chunk(a,2,1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[11., 12.],\n",
       "        [23., 24.]])"
      ]
     },
     "execution_count": 50,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.Tensor([[11,12],[23,24]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[11., 11.],\n",
       "        [24., 23.]])"
      ]
     },
     "execution_count": 51,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.gather(torch.Tensor([[11,12],[23,24]]), 1, \n",
    "             torch.LongTensor([[0,0],[1,0]]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0, 0],\n",
       "        [1, 0]])"
      ]
     },
     "execution_count": 52,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    " torch.LongTensor([[0,0],[1,0]]) \n",
    "#the 1D tensor containing the indices to index"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[-0.9605,  0.3085, -1.0552, -0.0221],\n",
      "        [ 0.9325, -0.5172, -0.0135,  0.6489],\n",
      "        [ 0.3539, -0.3596, -0.8797,  1.0190],\n",
      "        [ 0.9964,  0.5531,  0.3831, -0.3234]])\n"
     ]
    }
   ],
   "source": [
    "a = torch.randn(4, 4)\n",
    "print(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {},
   "outputs": [],
   "source": [
    "indices = torch.LongTensor([0, 2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.9605,  0.3085, -1.0552, -0.0221],\n",
       "        [ 0.3539, -0.3596, -0.8797,  1.0190]])"
      ]
     },
     "execution_count": 55,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.index_select(a, 0, indices)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 56,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.9605, -1.0552],\n",
       "        [ 0.9325, -0.0135],\n",
       "        [ 0.3539, -0.8797],\n",
       "        [ 0.9964,  0.3831]])"
      ]
     },
     "execution_count": 56,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.index_select(a, 1, indices)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 57,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0],\n",
       "        [2]])"
      ]
     },
     "execution_count": 57,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#identify null input tensors using nonzero function\n",
    "torch.nonzero(torch.tensor([10,00,23,0,0.0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0],\n",
       "        [2]])"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.nonzero(torch.Tensor([10,00,23,0,0.0]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([12, 21]), tensor([34, 32]), tensor([45, 54]), tensor([56, 65]))"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# splitting the tensor into small chunks\n",
    "torch.split(torch.tensor([12,21,34,32,45,54,56,65]),2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([12, 21, 34]), tensor([32, 45, 54]), tensor([56, 65]))"
      ]
     },
     "execution_count": 58,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# splitting the tensor into small chunks\n",
    "torch.split(torch.tensor([12,21,34,32,45,54,56,65]),3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 59,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[[0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.]],\n",
       "\n",
       "        [[0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.]],\n",
       "\n",
       "        [[0., 0., 0., 0.],\n",
       "         [0., 0., 0., 0.]]])"
      ]
     },
     "execution_count": 59,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.zeros(3,2,4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([3, 2, 4])"
      ]
     },
     "execution_count": 60,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.zeros(3,2,4).size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 61,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#how to reshape the tensors along a new dimension"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.2021,  0.9669, -0.9755, -0.3235,  0.7023],\n",
       "        [-0.2164,  0.3371,  0.3157, -0.6900,  1.2603],\n",
       "        [ 0.9157,  0.4921, -0.9229,  0.9070,  0.3861],\n",
       "        [ 1.2092,  0.8473, -0.5978,  0.3835,  0.4785]])"
      ]
     },
     "execution_count": 62,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 63,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.2021, -0.2164,  0.9157,  1.2092],\n",
       "        [ 0.9669,  0.3371,  0.4921,  0.8473],\n",
       "        [-0.9755,  0.3157, -0.9229, -0.5978],\n",
       "        [-0.3235, -0.6900,  0.9070,  0.3835],\n",
       "        [ 0.7023,  1.2603,  0.3861,  0.4785]])"
      ]
     },
     "execution_count": 63,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.t() #transpose is one option to change the shape of the tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# transpose partially based on rows and columns"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 65,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.2021, -0.2164,  0.9157,  1.2092],\n",
       "        [ 0.9669,  0.3371,  0.4921,  0.8473],\n",
       "        [-0.9755,  0.3157, -0.9229, -0.5978],\n",
       "        [-0.3235, -0.6900,  0.9070,  0.3835],\n",
       "        [ 0.7023,  1.2603,  0.3861,  0.4785]])"
      ]
     },
     "execution_count": 65,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x.transpose(1,0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# how to remove a dimension from a tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.2021,  0.9669, -0.9755, -0.3235,  0.7023],\n",
       "        [-0.2164,  0.3371,  0.3157, -0.6900,  1.2603],\n",
       "        [ 0.9157,  0.4921, -0.9229,  0.9070,  0.3861],\n",
       "        [ 1.2092,  0.8473, -0.5978,  0.3835,  0.4785]])"
      ]
     },
     "execution_count": 67,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 68,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([-0.2021, -0.2164,  0.9157,  1.2092]),\n",
       " tensor([0.9669, 0.3371, 0.4921, 0.8473]),\n",
       " tensor([-0.9755,  0.3157, -0.9229, -0.5978]),\n",
       " tensor([-0.3235, -0.6900,  0.9070,  0.3835]),\n",
       " tensor([0.7023, 1.2603, 0.3861, 0.4785]))"
      ]
     },
     "execution_count": 68,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.unbind(x,1) #dim=1 removing a column"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(tensor([-0.2021,  0.9669, -0.9755, -0.3235,  0.7023]),\n",
       " tensor([-0.2164,  0.3371,  0.3157, -0.6900,  1.2603]),\n",
       " tensor([ 0.9157,  0.4921, -0.9229,  0.9070,  0.3861]),\n",
       " tensor([ 1.2092,  0.8473, -0.5978,  0.3835,  0.4785]))"
      ]
     },
     "execution_count": 69,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.unbind(x) #dim=0 removing a row"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.2021,  0.9669, -0.9755, -0.3235,  0.7023],\n",
       "        [-0.2164,  0.3371,  0.3157, -0.6900,  1.2603],\n",
       "        [ 0.9157,  0.4921, -0.9229,  0.9070,  0.3861],\n",
       "        [ 1.2092,  0.8473, -0.5978,  0.3835,  0.4785]])"
      ]
     },
     "execution_count": 70,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 71,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#how to compute the basic mathematrical functions"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([10., 23.,  3.])"
      ]
     },
     "execution_count": 72,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.abs(torch.FloatTensor([-10, -23, 3.000]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 73,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[19.7979, 20.9669, 19.0245, 19.6765, 20.7023],\n",
       "        [19.7836, 20.3371, 20.3157, 19.3100, 21.2603],\n",
       "        [20.9157, 20.4921, 19.0771, 20.9070, 20.3861],\n",
       "        [21.2092, 20.8473, 19.4022, 20.3835, 20.4785]])"
      ]
     },
     "execution_count": 73,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#adding value to the existing tensor, scalar addition\n",
    "torch.add(x,20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 74,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.2021,  0.9669, -0.9755, -0.3235,  0.7023],\n",
       "        [-0.2164,  0.3371,  0.3157, -0.6900,  1.2603],\n",
       "        [ 0.9157,  0.4921, -0.9229,  0.9070,  0.3861],\n",
       "        [ 1.2092,  0.8473, -0.5978,  0.3835,  0.4785]])"
      ]
     },
     "execution_count": 74,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 75,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.4043,  1.9337, -1.9509, -0.6470,  1.4047],\n",
       "        [-0.4327,  0.6743,  0.6314, -1.3800,  2.5205],\n",
       "        [ 1.8315,  0.9843, -1.8458,  1.8140,  0.7723],\n",
       "        [ 2.4184,  1.6946, -1.1956,  0.7671,  0.9570]])"
      ]
     },
     "execution_count": 75,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# scalar multiplication\n",
    "torch.mul(x,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.2021,  0.9669, -0.9755, -0.3235,  0.7023],\n",
       "        [-0.2164,  0.3371,  0.3157, -0.6900,  1.2603],\n",
       "        [ 0.9157,  0.4921, -0.9229,  0.9070,  0.3861],\n",
       "        [ 1.2092,  0.8473, -0.5978,  0.3835,  0.4785]])"
      ]
     },
     "execution_count": 76,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# how do we represent the equation in the form of a tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# y = intercept + (beta * x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([-0.4292])"
      ]
     },
     "execution_count": 79,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "intercept = torch.randn(1)\n",
    "intercept"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.7945,  0.7653],\n",
       "        [ 0.5497,  0.8854]])"
      ]
     },
     "execution_count": 80,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.randn(2, 2)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7456"
      ]
     },
     "execution_count": 81,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "beta = 0.7456\n",
    "beta"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.5924,  0.5706],\n",
       "        [ 0.4098,  0.6602]])"
      ]
     },
     "execution_count": 82,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.mul(x,beta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 83,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.1145,  0.4453],\n",
       "        [ 0.2297,  0.5655]])"
      ]
     },
     "execution_count": 83,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.add(x,beta,intercept)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 0.3410, -0.3284],\n",
       "        [-0.2359, -0.3800]])"
      ]
     },
     "execution_count": 84,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.mul(intercept,x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.5924,  0.5706],\n",
       "        [ 0.4098,  0.6602]])"
      ]
     },
     "execution_count": 85,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.mul(x,beta)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 86,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.2514,  0.2422],\n",
       "        [ 0.1739,  0.2802]])"
      ]
     },
     "execution_count": 86,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "## y = intercept + (beta * x)\n",
    "torch.add(torch.mul(intercept,x),torch.mul(x,beta)) # tensor y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 87,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.1117, -0.4966,  0.1631, -0.8817,  0.0539],\n",
       "        [ 0.6684, -0.0597, -0.4675, -0.2153, -0.7141],\n",
       "        [-1.0831, -0.5547,  0.9717, -0.5150,  1.4255],\n",
       "        [ 0.7987, -1.4949,  1.4778, -0.1696, -0.9919],\n",
       "        [-1.4569,  0.2563, -0.4030,  0.4195,  0.9380]])"
      ]
     },
     "execution_count": 87,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# how to round up tensor values\n",
    "torch.manual_seed(1234)\n",
    "torch.randn(5,5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 88,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0., -0.,  1., -0.,  1.],\n",
       "        [ 1., -0., -0., -0., -0.],\n",
       "        [-1., -0.,  1., -0.,  2.],\n",
       "        [ 1., -1.,  2., -0., -0.],\n",
       "        [-1.,  1., -0.,  1.,  1.]])"
      ]
     },
     "execution_count": 88,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.manual_seed(1234)\n",
    "torch.ceil(torch.randn(5,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 89,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1., -1.,  0., -1.,  0.],\n",
       "        [ 0., -1., -1., -1., -1.],\n",
       "        [-2., -1.,  0., -1.,  1.],\n",
       "        [ 0., -2.,  1., -1., -1.],\n",
       "        [-2.,  0., -1.,  0.,  0.]])"
      ]
     },
     "execution_count": 89,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.manual_seed(1234)\n",
    "torch.floor(torch.randn(5,5))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 90,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.3000, -0.3000,  0.0000, -0.3000,  0.0000],\n",
       "        [ 0.0000, -0.3000, -0.3000, -0.3000, -0.3000],\n",
       "        [-0.3000, -0.3000,  0.0000, -0.3000,  0.4000],\n",
       "        [ 0.0000, -0.3000,  0.4000, -0.3000, -0.3000],\n",
       "        [-0.3000,  0.0000, -0.3000,  0.0000,  0.0000]])"
      ]
     },
     "execution_count": 90,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# truncate the values in a range say 0,1\n",
    "torch.manual_seed(1234)\n",
    "torch.clamp(torch.floor(torch.randn(5,5)), min=-0.3, max=0.4)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.3000, -0.3000,  0.0000, -0.3000,  0.0000],\n",
       "        [ 0.0000, -0.3000, -0.3000, -0.3000, -0.3000],\n",
       "        [-0.3000, -0.3000,  0.0000, -0.3000,  1.0000],\n",
       "        [ 0.0000, -0.3000,  1.0000, -0.3000, -0.3000],\n",
       "        [-0.3000,  0.0000, -0.3000,  0.0000,  0.0000]])"
      ]
     },
     "execution_count": 91,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#truncate with only lower limit\n",
    "torch.manual_seed(1234)\n",
    "torch.clamp(torch.floor(torch.randn(5,5)), min=-0.3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 92,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.0000, -1.0000,  0.0000, -1.0000,  0.0000],\n",
       "        [ 0.0000, -1.0000, -1.0000, -1.0000, -1.0000],\n",
       "        [-2.0000, -1.0000,  0.0000, -1.0000,  0.3000],\n",
       "        [ 0.0000, -2.0000,  0.3000, -1.0000, -1.0000],\n",
       "        [-2.0000,  0.0000, -1.0000,  0.0000,  0.0000]])"
      ]
     },
     "execution_count": 92,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#truncate with only upper limit\n",
    "torch.manual_seed(1234)\n",
    "torch.clamp(torch.floor(torch.randn(5,5)), max=0.3)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-7.9450,  7.6528],\n",
       "        [ 5.4968,  8.8545]])"
      ]
     },
     "execution_count": 93,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#scalar division\n",
    "torch.div(x,0.10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 94,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.4518, 2.1496],\n",
       "        [1.7327, 2.4241]])"
      ]
     },
     "execution_count": 94,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#compute the exponential of a tensor\n",
    "torch.exp(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.4518, 2.1496],\n",
       "        [1.7327, 2.4241]])"
      ]
     },
     "execution_count": 95,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.exp(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 96,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#how to get the fractional portion of each tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 97,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[ 9.2055, 10.7653],\n",
       "        [10.5497, 10.8854]])"
      ]
     },
     "execution_count": 97,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.add(x,10)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 98,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.2055, 0.7653],\n",
       "        [0.5497, 0.8854]])"
      ]
     },
     "execution_count": 98,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.frac(torch.add(x,10))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 99,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# compute the log of the values in a tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 100,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.7945,  0.7653],\n",
       "        [ 0.5497,  0.8854]])"
      ]
     },
     "execution_count": 100,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 101,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[    nan, -0.2675],\n",
       "        [-0.5984, -0.1217]])"
      ]
     },
     "execution_count": 101,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.log(x) #log of negatives are nan"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 102,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.6312, 0.5857],\n",
       "        [0.3021, 0.7840]])"
      ]
     },
     "execution_count": 102,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# to rectify the negative values do a power tranforamtion\n",
    "torch.pow(x,2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 103,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# rounding up similar to numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.7945,  0.7653],\n",
       "        [ 0.5497,  0.8854]])"
      ]
     },
     "execution_count": 104,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 105,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.,  1.],\n",
       "        [ 1.,  1.]])"
      ]
     },
     "execution_count": 105,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np.round(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 106,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-1.,  1.],\n",
       "        [ 1.,  1.]])"
      ]
     },
     "execution_count": 106,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.round(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 107,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# how to compute the sigmoid of the input tensor"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 108,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.7945,  0.7653],\n",
       "        [ 0.5497,  0.8854]])"
      ]
     },
     "execution_count": 108,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 109,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[0.3112, 0.6825],\n",
       "        [0.6341, 0.7080]])"
      ]
     },
     "execution_count": 109,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.sigmoid(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 110,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# finding the square root of the values"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 111,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[-0.7945,  0.7653],\n",
       "        [ 0.5497,  0.8854]])"
      ]
     },
     "execution_count": 111,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "tensor([[   nan, 0.8748],\n",
       "        [0.7414, 0.9410]])"
      ]
     },
     "execution_count": 112,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.sqrt(x)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
